package chapter5.section4;

import java.util.ArrayList;
import java.util.List;

//程序5-9：修改后能够保存业务信息的Accout类
public class Account0509 {
    // 关于集合类List及ArrayList请参考第8章
    private final List<Transaction> trans = new ArrayList<Transaction>();
    private String id;
    private String name;
    private int balance;

    public Account0509(String id, String name, int balance) {
        super();
        this.id = id;
        this.name = name;
        this.balance = balance;
    }

    private Account0509() {
        super();
    }

    public static void main(String[] args) {
        Account0509 a = new Account0509("001", "鲁宁", 1000);
        a.withdraw(100);
        a.deposit(150);
        a.showHistory();
    }

    // 一个存款方法，将存款额增加到余额上
    public void deposit(int amount) {
        this.balance += amount;
        // 创建一个业务对象保存到集合中
        trans.add(new Transaction(Transaction.TYPE_DEPOSIT, amount));
    }

    // 一个取款方法，将存款额从余额减去
    public void withdraw(int amount) {
        this.balance -= amount;
        trans.add(new Transaction(Transaction.TYPE_WITHDRAW, amount));
    }

    // 返回一个账户的当前余额
    public int getBalance() {
        return this.balance;
    }

    // 显示集合中保存的当前账户的所有业务记录
    public void showHistory() {
        System.out.println("账户:" + this.name);
        for (int i = 0; i < trans.size(); i++) {
            System.out.println(trans.get(i));
        }
    }
}
